from base import _PathHelper, _YamlHelper, build_request, Json
from page import Api


class NspUserApi(Api):
    """
    用途：在接口端后台登录和前台登录方法
    作者：曹峰
    时间：2019-5-6
    邮箱：243934857@qq.com
    """

    file = _PathHelper().get_actual_path_by_current_file(__file__, 'nsp.yaml')
    __login_yaml = _YamlHelper().get_config_as_dict(file)['NspUserApi']
    _get_token_sign = ()
    _get_web_token_sign = ()

    def login_admin(self, sign, account, password):
        """
        后台管理员登录
        :param sign: 管理员签名
        :param account: 管理员用户名
        :param password:登录密码
        :return:
                ret
                data
                   result
                       is_login
                       uid
                       token
                       status
                   code
                   msg
                msg
        """
        url = self._base_url
        login_url = '%s?s=%s' % (url, self.__login_yaml['LOGIN_URL'])
        login_data = {'sign': sign, 'account': account, 'password': password}
        login_req = build_request(login_url, 'p', params=login_data)
        self._set_token(login_req.json_dict, sign)

        return self._get_detail_resp(login_req.json_dict)

    def _get_parse_http_resp(self, json_dict):
        """
        解析http基本返回值
        :param json_dict:
        :return:data为空返回ret、data、msg；
                 data不为空返回ret、data、data/result、data/code、data/msg、msg
        """
        basic_resp = {}

        basic_resp[self.__login_yaml['BASIC_RET']] = Json.parse_json_dict_value(
            json_dict,
            data_key=self.__login_yaml[
                'BASIC_RET']
        )

        basic_resp[self.__login_yaml['BASIC_DATA']] = Json.parse_json_dict_value(
            json_dict,
            data_key=self.__login_yaml[
                'BASIC_DATA']
        )

        basic_resp[self.__login_yaml['BASIC_MSG']] = Json.parse_json_dict_value(
            json_dict,
            data_key=self.__login_yaml[
                'BASIC_MSG']
        )

        if basic_resp[self.__login_yaml['BASIC_DATA']]:
            basic_resp[self.__login_yaml['BASIC_RESULT']] = Json.parse_json_dict_value(
                json_dict,
                data_key=self.__login_yaml[
                    'LOGIN_DATAKEY']
            )

            basic_resp[self.__login_yaml['BASIC_CODE']] = Json.parse_json_dict_value(
                json_dict,
                data_key=self.__login_yaml[
                    'GET_CODE']
            )

            basic_resp[self.__login_yaml['BASIC_DATA_MSG']] = Json.parse_json_dict_value(
                json_dict,
                data_key=self.__login_yaml[
                    'GET_MSG']
            )

        return basic_resp

    def _get_detail_resp(self, json_dict):
        """
        解析json_dict
        :param json_dict:
        :return:返回基本值ret、data、msg和需要的is_login、uid、status
        """
        detail_resp = self._get_parse_http_resp(json_dict)

        detail_resp['is_login'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self.__login_yaml[
                'LOGIN_DATAKEY'],
            index=self.__login_yaml[
                'LOGIN_INDEX'],
            sub_key=self.__login_yaml[
                'LOGIN_SUBKEY']
        )

        detail_resp['uid'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self.__login_yaml[
                'UID_SUBKEY'],
            index=self.__login_yaml[
                'UID_INDEX'],
            sub_key=self.__login_yaml[
                'UID_SUBKEY']
        )

        detail_resp['status'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self.__login_yaml[
                'STATUS_DATAKEY'],
            index=self.__login_yaml[
                'STATUS_INDEX'],
            sub_key=self.__login_yaml[
                'STATUS_SUBKEY']
        )

        detail_resp['json_dict'] = json_dict

        return detail_resp

    def _set_token(self, json_dict, sign):
        """
        解析json_dict,获取token与sign组成元组
        :param json_dict:
        :param sign:
        :return:
        """
        token = Json.parse_json_dict_value(
            json_dict, data_key=self.__login_yaml[
                'TOKEN_DATAKEY'],
            index=self.__login_yaml[
                'TOKEN_INDEX'],
            sub_key=self.__login_yaml[
                'TOKEN_SUBKEY']
        )

        self._get_token_sign = (sign, token)

        return self._get_token_sign

    def _get_to_token_sign(self, sign, token):
        """
        存取sign和token
        :param sign:
        :param token:
        :return:sign，token
        """
        if not sign:
            sign = self._get_token_sign[0]
        if not token:
            token = self._get_token_sign[1]

        return sign, token

    def login_web(self, sign, account, password):
        """
        前台买家登录
        :param sign: 签名
        :param account: 用户名
        :param password: 密码
        :return:
                ret
                data
                   result
                       is_login
                       uid
                       token
                       status
                   code
                   msg
                msg
        """
        url = '%s?service=%s' % (self._base_url, self.__login_yaml['MEMBRE_URL'])
        data = {'sign': sign, 'account': account, 'password': password}
        req = build_request(url=url, method='p', params=data)
        self._set_web_sign_token(req.json_dict, sign)

        return self._get_web_detail_resp(req.json_dict)

    def _set_web_sign_token(self, json_dict, sign):
        """
        解析获取token
        :param json_dict:
        :return: 返回元组：sign,token
        """
        token = Json.parse_json_dict_value(
            json_dict, data_key=self.__login_yaml[
                'WEB_TOKEN_DATAKEY'],
            index=self.__login_yaml[
                'WEB_TOKEN_INDEX'],
            sub_key=self.__login_yaml[
                'WEB_TOKEN_SUBKEY']
        )

        self._get_web_token_sign = (sign, token)

        return self._get_web_token_sign

    def _get_to_web_sign_token(self, sign, token):
        """
        存取web元组：sign，token
        :return:sign，token
        """
        if not sign:
            sign = self._get_web_token_sign[0]
        if not token:
            token = self._get_web_token_sign[1]

        return sign, token

    def _get_web_detail_resp(self, json_dict):
        """
        解析json_dict
        :param json_dict:
        :return:
                result
                      is_login
                      uid
                      token
                      status
                code
                msg
        """
        web_detail_resp = self._get_parse_http_resp(json_dict)

        web_detail_resp['is_login'] = Json.parse_json_dict_value(
            json_dict,
            data_key=
            self.__login_yaml[
                'IS_LOGIN_DATAKEY'],
            index=self.__login_yaml[
                'IS_LOGIN_INDEX'],
            sub_key=self.__login_yaml[
                'IS_LOGIN_SUBKEY']
        )

        web_detail_resp['uid'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self.__login_yaml[
                'WEB_UID_DATAKEY'],
            index=self.__login_yaml[
                'WEB_UID_INDEX'],
            sub_key=self.__login_yaml[
                'WEB_UID_SUBKEY']
        )

        web_detail_resp['status'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self.__login_yaml[
                'WEB_STATUS_DATAKEY'],
            index=self.__login_yaml[
                'WEB_STATUS_INDEX'],
            sub_key=self.__login_yaml[
                'WEB_STATUS_SUBKEY']
        )

        return web_detail_resp
